package org.jeecg.modules.demo3.fly.controller.qun;

import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo3.fly.entity.AxhkYwAgrplane;
import org.jeecg.modules.demo3.fly.entity.assist.qun.AxhkYwAgrplanes;
import org.jeecg.modules.demo3.fly.entity.assist.qun.Fluck;
import org.jeecg.modules.demo3.fly.entity.assist.qun.Quantity;
import org.jeecg.modules.demo3.fly.entity.assist.qun.User;
import org.jeecg.modules.demo3.fly.mapper.AxhkYwAgrplaneMapper;
import org.jeecg.modules.demo3.fly.service.qun.QunService;
import org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wjn
 * @version V1.0
 * @date 2021/6/30 14:12
 * @Copyright © 2021/6/30 深圳卓翼航空无人机有限公司
 */
@RestController
@RequestMapping("/fly/qun")
public class QunController {

    @Autowired
    private QunService qunService;

    /*
    * 创建 群
    * String number  创建用户的电话号
    * String name   群名
    * */
    @PostMapping("getCreatFluck")
    public Result<?> getCreatFluck(String number,String fName){
        String msging = qunService.getCreatByFluck(number,fName);
        switch (msging){
            case "A" :
                return Result.OKF("群组未创建成功!");
            case "B" :
                return Result.OKF("该群名已经存在，请重新创建!");
            case "C" :
                return Result.OKF("请正确填写群名、电话号为空!");
            default:
                return Result.OK("创建成功！",msging);
        }
    }

    /*
    * 添加 队员
    * String number 用户电话号码
    * String fId 群id
    * */
    @PostMapping("getInsertTeam")
    public Result<?> getInsertTeam(String uId,String number,String fId){
        String msging = qunService.getInsertByTeam(uId,number,fId);
        switch(msging){
            case "A" :
                return Result.OKF("该用户已经存在于此群!");
            case "B":
                return Result.OKF("该用户未注册!");
            case "C":
                return Result.OKF("用户或者所要添加的群，不能为为空!");
            case "D":
                return Result.OKF("你不是队长没有权限添加队员! ");
            default:
                return Result.OKT(msging);
        }
    }

    /*
    * 移除队员
    * String uId 队员id
    * String fId 移除的群id
    * */
    @GetMapping("getDeleteTeam")
    public Result<?> getDeleteTeam(String uId1,String uId,String fId){
        String msging = qunService.getDeleteByTeam(uId1,uId,fId);
        switch(msging){
            case "A" :
                return Result.OKF("不能移除，该用户是队长!");
            case "B" :
                return Result.OKF("该用户或者群不存在!");
            case "C" :
                return Result.OKF("该用户不在该群中!");
            case "D":
                return Result.OKF("你不是队长没有权限添加队员! ");
            default:
                return Result.OKT(msging);
        }
    }

    /*
    * 转换队长
    * String uId 需要成为队长的用户
    * String fId 群id
    * */
    @GetMapping("getUpdateTeam")
    public Result<?> getUpdateTeam(String uId,String fId){
        String msging = qunService.getUpdateByTeam(uId,fId);
        switch(msging) {
            case "A":
                return Result.OKF("该用户不存在此群！");
            case "B":
                return Result.OKF("该用户已经是队长了！");
            default:
                return Result.OKT(msging);
        }
    }

    /*
    * 添加设备
    * String fId
    * int serialNumber 设备序列号
    * */
    @PostMapping("getAddFacility")
    public Result<?> getAddFacility(String fId,int serialNumber){
        String msging = qunService.getAddByFacility(fId,serialNumber);
        Result<?> result = getAllFacility(fId);
        List<AxhkYwAgrplanes> axhkYwAgrplanes = (List<AxhkYwAgrplanes>) result.getResult();
        switch(msging){
            case "A":
                return Result.OKF("该设备已经存在于群中(该序列号不在设备中)!");
            case "C":
                return Result.OKF("请正确选择设备!");
            default:
                return Result.OK(axhkYwAgrplanes);
        }
    }

    /*
     * 通过群将群中的设备给某个用户
     * String uId1 队长ID
     * String uId2 队员ID
     * int serialNumber  设备序列号
     * String fId 群ID
     * */
    @GetMapping("getByFacilityUser")
    public Result<?> getByFacilityUser(String uId1, String uId2, int serialNumber,String fId){
        String msging = qunService.getAddUserOnFacility(uId1,uId2,serialNumber,fId);
        Result<?> result = getAllFacility(fId);
        List<AxhkYwAgrplanes> axhkYwAgrplanes = (List<AxhkYwAgrplanes>) result.getResult();
        switch(msging){
            case "A":
                return Result.OKF("该无人机正在连接！");
            case "B":
                return Result.OKF("你不是队长没有权限移除队员!");
            case "C":
                return Result.OKF("请正确选择被添加人员或设备!");
            default:
                return Result.OK(axhkYwAgrplanes);
        }
    }

    /*
     * 显示该群 所有设备
     * String fId 群ID
     * */
    @GetMapping("getAllFacility")
    public Result<?> getAllFacility(String fId){
        List<AxhkYwAgrplanes> axhkYwAgrplanes = qunService.getAllAgrplanes(fId);
        if(null != axhkYwAgrplanes){
            return Result.OK("获取成功!",axhkYwAgrplanes);
        }else{
            return Result.OKF("该群没有设备！");
        }
    }

    /*
    * 移除设备
    * String fId
    * int serialNumber 设备序列号
    * */
    @GetMapping("getDeleteFacility")
    public Result<?> getDeleteFacility(String uId,String fId,int serialNumber){
        String msging = qunService.getDeleteByFacility(uId,fId,serialNumber);
        switch(msging){
            case "A":
                return Result.OKF("请正确选择设备!");
            case "B":
                return Result.OKF("你不是队长没有权限移除队员!");
            case "C":
                return Result.OKF("该设备已经被移除!");
            default:
                return Result.OKT(msging);
        }
    }

    /*
    * 向移动端显示群信息 ①所有队员  ②所有设备
    * String fId
    * */
    @GetMapping("getAllFluck")
    public Result<?> getAllFluck(String uId){
        System.out.println("群列表!");
        List<Fluck> fluck = qunService.getByFluck(uId);
        if(null != fluck){
            return Result.OK("获取成功!",fluck);
        }else{
            return Result.OKF("未获取到!");
        }
    }

    /*
    * 获取该群的所有成员
    * String fId
    * */
    @GetMapping("getAllFluckUser")
    public Result<?> getAllFluckUser(String fId){
        List<User> users = qunService.getByUser(fId);
        if(null != users){
            return Result.OK("获取群成员列表成功！",users);
        }else{
            return Result.OKF("请正确选择群！");
        }
    }

    /*
    * 删除群
    * String uId
    * String fId
    * */
    @GetMapping("getDeleteFluck")
    public Result<?> getDeleteFluck(String uId,String fId){
        String msging = qunService.getByDeleteFluck(uId,fId);
        switch(msging){
            case "A":
                return Result.OKF("你不是队长没有权限删除群");
            default:
                return Result.OKT(msging);
        }
    }

    /*
     * 通过群获取该群的 成员数量 任务数量 作业总面积
     * String fId
     * */
    @GetMapping("getByQuantitys")
    public Result<?> getByQuantitys(String fId){
        Quantity quantity = qunService.getByQuantity(fId);
        if(null != quantity){
            return Result.OK("获取成功!",quantity);
        }else{
            return Result.OKF("获取失败!");
        }
    }

    /*
     * 通过 飞机序列号跟用户id 查看飞机所属
     * Integer serialNumber
     * String uId
    * */
    @GetMapping("getByBoolean")
    public Result<?> getByBoolean(Integer serialNumber,String uId,String fId){
        String msging = qunService.getBoolean(serialNumber,uId);
        List<AxhkYwAgrplanes> allAgrplanes = qunService.getAllAgrplanes(fId);
        switch(msging){
            case "A":
                return Result.OKF("序列号为空！");
            case "B":
                return Result.OKF("未查到该设备!");
            case "C":
                return Result.OKF("不是该用户持有！");
            case "D":
                return Result.OKF("该设备未有持有人！");
            default:
                return Result.OK(msging,allAgrplanes);
        }
    }

    /*
     * 手机App断开连接后 修改连接状态
     * Integer serialNumber
    * */
    @GetMapping("getByUpdateAstate")
    public Result<?> getByUpdateAstate(Integer serialNumber,String fId){
        String msging = qunService.getUpdateAstate(serialNumber);
        List<AxhkYwAgrplanes> allAgrplanes = qunService.getAllAgrplanes(fId);
        switch(msging){
            case "B":
                return Result.OKT("连接状态!");
            case "C":
                return Result.OKF("序列号不能为空!");
            default:
                return Result.OK(msging,allAgrplanes);
        }
    }

    /*
     * 队员自己退群
     * String uId 退群人id
     * String fId 群id
    * */
    @GetMapping("getByExitFluck")
    public Result<?> getByExitFluck(String uId,String fId){
        String msging = qunService.getExitFluck(uId,fId);
        switch(msging){
            case "A":
                return Result.OKF("队长不可以退群!");
            case "B":
                return Result.OKT("退群成功!");
            default:
                return Result.OKT(msging);
        }
    }
}
